import { GAME_CONFIG } from './game-config.js';

/**
 * 玩家车辆类
 * @description 管理玩家车辆的所有属性和行为
 */
export class Player {
    constructor(canvas) {
        this.canvas = canvas;
        this.x = canvas.width / 2;
        this.y = canvas.height - 100;
        this.width = GAME_CONFIG.VEHICLE.WIDTH;
        this.height = GAME_CONFIG.VEHICLE.HEIGHT;
        this.angle = 0;
        this.maxSpeed = GAME_CONFIG.VEHICLE.MAX_SPEED;
        this.acceleration = GAME_CONFIG.VEHICLE.ACCELERATION;
        this.deceleration = GAME_CONFIG.VEHICLE.DECELERATION;
        this.turnSpeed = GAME_CONFIG.VEHICLE.TURN_SPEED;
        this.friction = GAME_CONFIG.VEHICLE.FRICTION;
    }

    /**
     * 重置车辆位置和状态
     */
    reset() {
        this.x = this.canvas.width / 2;
        this.y = this.canvas.height - 100;
        this.angle = 0;
    }

    /**
     * 更新车辆状态
     * @param {Object} keys - 按键状态
     * @param {number} speed - 当前速度
     */
    update(keys, speed) {
        this.updatePosition(speed);
        this.updateAngle(keys, speed);
        this.updateBoundaries();
    }

    /**
     * 更新车辆位置
     * @param {number} speed - 当前速度
     */
    updatePosition(speed) {
        this.x += Math.sin(this.angle) * speed * 0.1;
    }

    /**
     * 更新车辆角度
     * @param {Object} keys - 按键状态
     * @param {number} speed - 当前速度
     */
    updateAngle(keys, speed) {
        if (keys.ArrowLeft) {
            this.angle -= this.turnSpeed * (speed / 100);
        }
        if (keys.ArrowRight) {
            this.angle += this.turnSpeed * (speed / 100);
        }

        // 限制转向角度
        this.angle = Math.max(-Math.PI / 4, Math.min(this.angle, Math.PI / 4));
    }

    /**
     * 更新边界检测
     */
    updateBoundaries() {
        const roadLeft = this.canvas.width / 2 - GAME_CONFIG.ROAD.WIDTH / 2 + this.width / 2;
        const roadRight = this.canvas.width / 2 + GAME_CONFIG.ROAD.WIDTH / 2 - this.width / 2;
        this.x = Math.max(roadLeft, Math.min(this.x, roadRight));
    }

    /**
     * 绘制车辆
     * @param {CanvasRenderingContext2D} ctx - Canvas上下文
     */
    draw(ctx) {
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.angle);

        // 绘制车身
        ctx.fillStyle = GAME_CONFIG.COLORS.PLAYER;
        ctx.fillRect(-this.width / 2, -this.height / 2, this.width, this.height);

        // 绘制车窗
        ctx.fillStyle = GAME_CONFIG.COLORS.WINDOW;
        ctx.fillRect(-this.width / 2 + 5, -this.height / 2 + 5, this.width - 10, this.height / 3);

        // 绘制车轮
        ctx.fillStyle = GAME_CONFIG.COLORS.WHEEL;
        ctx.fillRect(-this.width / 2 - 5, -this.height / 2 + 5, 8, 15);
        ctx.fillRect(this.width / 2 - 3, -this.height / 2 + 5, 8, 15);
        ctx.fillRect(-this.width / 2 - 5, this.height / 2 - 20, 8, 15);
        ctx.fillRect(this.width / 2 - 3, this.height / 2 - 20, 8, 15);

        ctx.restore();
    }

    /**
     * 获取车辆边界
     * @returns {Object} 车辆边界信息
     */
    getBounds() {
        return {
            left: this.x - this.width / 2,
            right: this.x + this.width / 2,
            top: this.y - this.height / 2,
            bottom: this.y + this.height / 2
        };
    }

    /**
     * 检测与其他对象的碰撞
     * @param {Object} object - 要检测的对象
     * @returns {boolean} 是否发生碰撞
     */
    checkCollision(object) {
        const bounds = this.getBounds();
        return (
            bounds.bottom > object.y &&
            bounds.top < object.y + object.height &&
            bounds.right > object.x - object.width / 2 &&
            bounds.left < object.x + object.width / 2
        );
    }
} 